package com.duobaoyu.dby.supply.common.biz.serializer;

import com.duobaoyu.dby.common.core.constant.CommonConstants;
import com.duobaoyu.dby.common.core.constant.StringPool;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Objects;

/**
 * @author jz
 * @version 1.0
 * @date 2022/6/24 11:59
 */
public class BigDecimalSerializer extends JsonSerializer<BigDecimal> {

	@Override
	public void serialize(BigDecimal value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
		if (Objects.nonNull(value)) {
			BigDecimal number = value.setScale(CommonConstants.TWO, BigDecimal.ROUND_HALF_UP);
			gen.writeString(number.toPlainString());
		} else {
			gen.writeString(StringPool.EMPTY);
		}
	}

}
